package com.khoayang.littlefighterchaos.game;

import java.util.ArrayList;

import org.cocos2d.actions.base.CCAction;
import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.interval.CCAnimate;
import org.cocos2d.nodes.CCAnimation;

public class State {
	private ArrayList<BaseSkill> _SkillList;
	private BaseSkill _CurrentSkill;
	private BaseSkill _PreSkill;
	private CCAction _CurrentAction;
	private CCAction _PreviousAction;
	private ArrayList<Command> _Commands;
	private String _Name;
	private float _AutoMPRegen;

	public State() {

	}

	public String get_Name() {
		return _Name;
	}

	public void set_Name(String _Name) {
		this._Name = _Name;
	}
	
	
	public float get_AutoMPRegen() {
		return _AutoMPRegen;
	}

	public void set_AutoMPRegen(float _AutoMPRegen) {
		this._AutoMPRegen = _AutoMPRegen;
	}
	
	public ArrayList<Command> get_Commands() {
		return _Commands;
	}

	public void set_Commands(ArrayList<Command> _Commands) {
		this._Commands = _Commands;
	}
	
	public ArrayList<BaseSkill> get_SkillList() {
		return _SkillList;
	}

	public void set_SkillList(ArrayList<BaseSkill> skills) {
		this._SkillList = skills;
	}

	public CCAction getAnimationSkill(String actionName) {
		BaseSkill skill = getSkill(actionName);
		if (skill != null) {
			int skillIndex = getSkillIndex(actionName);
			CCAnimation animation = skill.get_Animation();
			CCAnimate animate = CCAnimate.action(skill.get_AnimationDelay(),
					animation, true);
			animate.setTag(skillIndex);
			if (skill instanceof AttackingSkill) {
				AttackingSkill attackSkill = (AttackingSkill) skill;
				if (attackSkill.is_IsSpecial()) {
					
					return animate;
				}
			}else if(skill instanceof BeHitSkill || skill instanceof DyingSkill || skill instanceof TransformingSkill || skill instanceof JumpingSkill){
				return animate;
			}
			CCRepeatForever repeatAnimate = CCRepeatForever.action(animate);
			repeatAnimate.setTag(skillIndex);
			return repeatAnimate;
		}
		return null;
	}

	private int getSkillIndex(String actionName) {
		for (int i = 0; i < _SkillList.size(); i++) {
			BaseSkill skill = _SkillList.get(i);
			if (skill.get_SkillName().compareTo(actionName) == 0) {
				return i;
			}
		}
		return -1;
	}

	public BaseSkill getSkill(String actionName) {
		for (int i = 0; i < _SkillList.size(); i++) {
			BaseSkill skill = _SkillList.get(i);
			if (skill.get_SkillName().compareTo(actionName) == 0) {
				return skill;
			}
		}
		return null;
	}

	public BaseSkill get_CurrentSkill() {
		return _CurrentSkill;
	}

	public void set_CurrentSkill(BaseSkill _CurrentSkill) {
		this._CurrentSkill = _CurrentSkill;
	}

	public BaseSkill get_PreSkill() {
		return _PreSkill;
	}

	public void set_PreSkill(BaseSkill _PreSkill) {
		this._PreSkill = _PreSkill;
	}

	public CCAction get_CurrentAction() {
		return _CurrentAction;
	}

	public void set_CurrentAction(CCAction currentAction) {
		this._CurrentAction = currentAction;
		if (currentAction != null)
			this._CurrentSkill = _SkillList.get(currentAction.getTag());
		else
			_CurrentSkill = null;
	}

	public CCAction get_PreviousAction() {
		return _PreviousAction;
	}

	public void set_PreviousAction(CCAction previousAction) {
		this._PreviousAction = previousAction;
		if (previousAction != null)
			this._PreSkill = _SkillList.get(previousAction.getTag());
		else
			_PreSkill = null;
	}

	public int getMPConsume(String animationName) {
		BaseSkill skill = getSkill(animationName);
		if(skill instanceof AttackingSkill){
			return ((AttackingSkill) skill).get_MPUse();
		}
		return 0;
	}

	public State clone() {
		State state = new State();
		state.set_SkillList(_SkillList);
		state.set_Commands(_Commands);
		state.set_Name(_Name);
		state.set_AutoMPRegen(_AutoMPRegen);
		state.set_CurrentAction(null);
		state.set_PreviousAction(null);
		return state;
	}
}
